<template>
    <SearchForm :FormValue="DS.searchForm" :FormConfig />

    <a-card>
        <a-spin :spinning="DS.loading">
            <a-table bordered :scroll :pagination="DS.searchForm" :dataSource="DS.tableData" :columns>
                <template #bodyCell="{ text, record, index, column }">
                    <template v-if="column.dataIndex === 'action'">
                        <a-popconfirm title="此操作不可逆，是否继续？" ok-text="确定" cancel-text="取消"
                            @confirm="record.illArea == '' ? DS.delDepartment(record.id) : DS.delWard(record.id)"
                            @cancel="EventBus.emit('CancleInfo')">
                            <Btn type="link">删除</Btn>
                        </a-popconfirm>
                    </template>
                </template>
            </a-table>
        </a-spin>
    </a-card>

    <ModalForm ref="addFormRef" type="add" :okFn="DS.addForm.illArea == '' ? DS.addDepartment : DS.addWard"
        :FormValue="DS.addForm" :FormConfig="addFormConfig" :rules="addRules" />
</template>
<script lang='ts' setup>

import { searchConfig, addConfig, columns } from '@/config/Department';
import { useDepartment } from '@/stores/Department';
import { onUnmounted, provide, ref } from 'vue';


import { createForm } from '@/utils/CreateForm';
import { createBtns } from '@/utils/createPermissionBtns';
import EventBus from '@/utils/EventBus';
import { useTableScroll } from '@/utils/GetTableScroll';

const { FormConfig } = new createForm(searchConfig, { labelAlign: 'right', labelCol: { span: 3 }, layout: 'inline' },)
const { FormConfig: addFormConfig, FormRules: addRules } = new createForm(addConfig)

const scroll = useTableScroll('searchCard')
const DS = useDepartment()
DS.getTableData()

const addFormRef = ref()

provide('permissionBtns', createBtns({
    query: () =>
        DS.searchForm.departmentWard ?
            DS.searchForm.departmentWard.length == 2 ? DS.getWardList() : DS.searchForm.departmentWard.length == 1 ? DS.getDepartment() : DS.getTableData() : DS.getTableData(),
    add: () => addFormRef.value.open = true
}))

onUnmounted(() => {
    DS.$reset()
})
</script>
<style lang='scss' scoped></style>